#!/bin/sh /etc/rc.common

START=90
STOP=10
USE_PROCD=1

start_service() {
    config_load "baidupcs-web"

    config_get "enabled" "config" "enabled"
    config_get "port" "config" "port"
    config_get "download_dir" "config" "download_dir"
    config_get "max_download_rate" "config" "max_download_rate" '0'
    config_get "max_upload_rate" "config" "max_upload_rate" '0'
    config_get "max_download_load" "config" "max_download_load" '1'
    config_get "max_parallel" "config" "max_parallel" '8'

    [ "x$enabled" == "x1" ] || return 0
    mkdir -p "${download_dir}"
    /usr/bin/baidupcs-web config set                    \
        --savedir               "${download_dir}"       \
        --max_download_rate     "${max_download_rate}"  \
        --max_upload_rate       "${max_upload_rate}"    \
        --max_download_load     "${max_download_load}"  \
        --max_parallel          "${max_parallel}"       \
        --max_upload_parallel   "${max_parallel}"       \
        >/dev/null 2>&1

    procd_open_instance
    procd_set_param command /usr/bin/baidupcs-web
    procd_append_param command web --port "${port}" --access 
    procd_set_param respawn
    procd_close_instance
}

service_triggers() {
    procd_add_reload_trigger "baidupcs-web"
}
